package com.sinosoft.dsp.security.account;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.sinosoft.dsp.platform.utils.MessageBundles;

/**
 * 验证两次密码输入是否相同
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-5-30 下午11:29:39
 * 
 */
@FacesValidator("confirmPassword")
public class ConfirmPasswordValidator implements Validator {

	@ManagedProperty(value = "#{messageBundles}")
	private MessageBundles messageBundles;

	private String password;

	private String confirmPassword;

	public void validate(final FacesContext ctx, final UIComponent form,
			final Object components) throws ValidatorException {
		if (password == null || confirmPassword == null) {
			return;
		}

		if (!password.equals(confirmPassword)) {
			throw new ValidatorException(new FacesMessage(messageBundles
					.getValue("account.confirm.passwordsDoNotMatch")));
		}
	}

}
